$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'bonusRule/sellerbonusrule/list',
        datatype: "json",
        colModel: [			
			{ label: '奖励金倍数', name: 'multiple', index: 'multiple', width: 80 },
			{ label: '开始时间', name: 'startTime', index: 'start_time', width: 80 }, 			
			{ label: '结束时间', name: 'endTime', index: 'end_time', width: 80 }, 			
			{ label: '创建人', name: 'creater', index: 'creater', width: 80 }, 			
			{ label: '创建时间', name: 'createrTime', index: 'creater_time', width: 80 }, 			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 15,
		rowList : [15,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		sellerBonusRule: {},
        loading: false,
		rules:{
            multiple:[
                { required: true, message: '请输入奖励金倍数', trigger: 'blur' },
                { type: 'number', message: '奖励金倍数必须为数值'}
            ],
		},
        startEndTime:[],
        pickerStartEndTime:{
            disabledDate(time){
                return time.getTime() < new Date( Date.now() - 24*60*60*1000);;
            },
        },
	},
	watch:{
        startEndTime: function (value) {
        	if (value && value.length == 2 ){
                this.sellerBonusRule.startTime = value[0];
                this.sellerBonusRule.endTime = value[1];
            }else {
                this.sellerBonusRule.startTime = null;
                this.sellerBonusRule.endTime = null;
            }
        }
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.sellerBonusRule = {};
            this.startEndTime = [];
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id);
		},
		saveOrUpdate: function (formName) {
			var url = vm.sellerBonusRule.id == null ? "bonusRule/sellerbonusrule/save" : "bonusRule/sellerbonusrule/update";
			if (this.sellerBonusRule.startTime == null || this.sellerBonusRule.endTime == null){
                this.$message.error('请选择起始时间 ! ');
                return false;
			}
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    $.ajax({
                        type: "POST",
                        url: baseURL + url,
                        contentType: "application/json",
                        data: JSON.stringify(vm.sellerBonusRule),
                        success: function(r){
                            if(r.code === 0){
                                alert('操作成功', function(index){
                                    vm.reload();
                                });
                            }else{
                                alert(r.msg);
                            }
                        }
                    });
                } else {
                    return false;
                }
            });

		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "bonusRule/sellerbonusrule/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "bonusRule/sellerbonusrule/info/"+id, function(r){
                vm.sellerBonusRule = r.sellerBonusRule;
                var temp = [];
                temp[0] = vm.sellerBonusRule.startTime;
                temp[1] = vm.sellerBonusRule.endTime;
                vm.startEndTime = temp;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
	}
});